<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Account extends CI_Controller {
	
	// public variable to store number of email active that send to user
	public $mail_count=0;
	
	public function __construct(){
		parent::__construct();
		if($this->session->userdata('simple_authentication_id')){
			$user_id=$this->session->userdata('simple_authentication_id');
			// assign value to count mail active in constructor method
			$this->mail_count=$this->mod_account->select_count_mail_active($user_id);
			 //$this->mail_count=$this->mod_globals->select_join_count(PREFIX_TABLE."product_owner_mailss", $arr_join = array(PREFIX_TABLE.'product_owners' => array('use_send_from_product_owner_id' => 'product_owner_id')), $join_type ="INNER",array('use_send_to_product_owner_id'=>$user_id,'mail_status'=>1));
		}
		
		// global set error form validation style
		$this -> form_validation -> set_error_delimiters('<p class="error">', '</p>');
	}
	public function index() {
		redirect("account/products");
	}
	
	// function for add product that user like  to their product favorite
	public function add_favorite_product(){
		$product_id=$this->input->post('product_item_id');
		
		// checking user have login before they can add product to their favorite
		if (!$this -> mod_account -> is_logged_in()) {
			echo "<div class='alert alert-info'>You must <a href='".base_url()."account/sign-in'>login</a> or <a href='".base_url()."account/sign-up'>create an account</a> to add this product to your favorite !</div>";
		}else{
			
			$user_id=$this->session->userdata('simple_authentication_id');
			
			// checking if exist product favorite that they have add before
			$is_exists_favorite_product=$this->mod_globals->check_data_exist(PREFIX_TABLE."favorite",array("use_product_id"=>$product_id,"use_product_owner_id"=>$user_id));
			if($is_exists_favorite_product){
				echo "<div class='alert alert-warning'>You have added this product to your favorite already ! Thank you .</div>";
			}else{
					
				// add product to user product favorite
				$isSaved=$this->mod_globals->insert(PREFIX_TABLE."favorite",array("use_product_id"=>$product_id,"use_product_owner_id"=>$user_id,"status"=>1,"created_date"=>null));
				if($isSaved){
					echo "<div class='alert alert-success'>Success : you have added this product to your favorite !</div>";
				}else{
					echo "<div class='alert alert-danger'>This product item was added to favorite fail !</div>";
				}	
			}
			
		}
	}
	
	// Function For list all product favorite of user that they have added to their favorite
	public function favorite() {
		$data['title'] = "favorites";
		$data['mail_counts']=$this->mail_count;
		$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");// select list all product category for browsing product by category (Most of function has this option)
		$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");// select list all product brand for browsing product by brand (Most of function has this option)
		
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$user_id=$this->session->userdata('simple_authentication_id');
		
		// Config data list with pagination
		$this->load->library('pagination');
		$config = array();
		// config url page for display data with your pagination
		$config["base_url"] = base_url() . "account/favorite";
		
		// select to count the total number of product favorite of user
		$config["total_rows"] = $this->mod_globals->select_join_count(PREFIX_TABLE."favorite", $arr_join = array(PREFIX_TABLE.'products' => array('use_product_id' => 'product_id'),PREFIX_TABLE.'product_owners' => array('use_product_owner_id' => 'product_owner_id')), $join_type ="INNER",array(PREFIX_TABLE.'favorite.use_product_owner_id'=>$user_id));
		$config["per_page"] = 10;
		$config["uri_segment"] = 3;
		$config['full_tag_open'] = '<h3 class="pagination">';
		$config['full_tag_close'] = '</h3>';
	 
	    $this->pagination->initialize($config);
	 
	    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		 
		 // select data to display with your pagination depend on your data with your paination config
	    $data['products']=$this->mod_globals->select_join_get_only_fields(PREFIX_TABLE."favorite", $arr_field=array(PREFIX_TABLE.'products.name AS pro_name',PREFIX_TABLE.'favorite.use_product_id','price','description',PREFIX_TABLE."product_images.name AS img_name","key_code"),$arr_join = array(PREFIX_TABLE.'products' => array('use_product_id' => 'product_id'),PREFIX_TABLE.'product_owners' => array('use_product_owner_id' => 'product_owner_id'),PREFIX_TABLE.'product_images' => array('use_product_id' => 'use_product_id')), $join_type ="INNER",array(PREFIX_TABLE.'favorite.use_product_owner_id'=>$user_id,PREFIX_TABLE."product_images.priority"=>1),$config["per_page"].",". $page,"favorite_id desc");
	    $data["pagination"] = $this->pagination->create_links();
		
		//$data['products']=$this->mod_globals->select_join(PREFIX_TABLE."favorite", $arr_join = array(PREFIX_TABLE.'products' => array('use_product_id' => 'product_id'),PREFIX_TABLE.'product_owners' => array('use_product_owner_id' => 'product_owner_id')), $join_type ="INNER",array('use_product_owner_id'=>$user_id),$config["per_page"].",". $page);
		$this -> load -> view(F_MASTER . 'index', $data);
	}

	// This function is used to remove product from your product favorite list
	public function delete_favorite_product(){
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$product_key=$this->uri->segment(3);
		$pro_id=$this->mod_globals->select_string(PREFIX_TABLE."products","product_id",array("key_code"=>$product_key));
		
		$user_id=$this->session->userdata('simple_authentication_id');
		$this->mod_globals->delete(PREFIX_TABLE."favorite",array('use_product_owner_id'=>$user_id,'use_product_id'=>$pro_id));
		
		$this -> session -> set_flashdata('success', 'Favorite product was deleted success!');
		redirect('account/favorite');
	}
	
	// This function used to  login to their account to control their product
	public function sign_in() {
		$data['title'] = "Sign In";
		if ($this -> mod_account -> is_logged_in()) {
			redirect("account");
		} else {
			$this -> form_validation -> set_rules('email', 'Email', 'callback_login_check');
			//$this -> form_validation -> set_error_delimiters("<div class='alert alert-danger'", "</div>");

			if ($this -> form_validation -> run() == FALSE) {
				$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
				$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
				$this -> load -> view(F_MASTER . 'index', $data);
			} else {
				redirect("account");
			}
		}
		
	}
	
	// function for checking is login success or not
	public function login_check($email) {
		$password = $this -> input -> post("password",TRUE);
		if (!$this -> mod_account -> login($email, $password)) {
			$this -> form_validation -> set_message('login_check', $this -> lang -> line('login_invalid_email_and_password'));
			return false;
		}
		return true;
	}

	// function for allow user to register with our website to post their product or get newsletter
	public function sign_up() {
		$data['title'] = "Create an account";
		
		$tbl_product_owners=PREFIX_TABLE."product_owners";
		
		//option for checking user input data with validation
		
		$this -> form_validation -> set_rules('first_name', 'First Name', 'trim|required|min_length[2]|max_length[20]|alpha');
		$this -> form_validation -> set_rules('last_name', 'Last Name', 'trim|required|min_length[2]|max_length[20]|alpha');
		$this -> form_validation -> set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[30]');
		$this -> form_validation -> set_rules('email', 'Email', "required|valid_email|is_unique[$tbl_product_owners.email]");
		$this -> form_validation -> set_rules('password', 'Password', 'trim|required|min_length[8]|max_length[50]|matches[confirm_password]');
		$this -> form_validation -> set_rules('confirm_password', 'Confirm password', 'trim|required|min_length[8]|max_length[50]');
		
		 
		$this->form_validation->set_rules('math_question', 'Math Question', 'trim|required|numeric|callback_math_question');
		

		if ($this -> form_validation -> run() === FALSE) {
			$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
			$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
			
			// Simple math question to protect spam when user register to our website to post job 
			if($this->session->userdata('questionOne') && $this->session->userdata('questionTwo')){
				$this->session->unset_userdata('questionOne');
				$this->session->unset_userdata('questionTwo');
			}
			
			$this->load->library('math_question');
		
			$questionOne = $this->math_question->questionOne();
			$questionTwo = $this->math_question->questionTwo();
	 
			$this->session->set_userdata('questionOne', $questionOne);
			$this->session->set_userdata('questionTwo', $questionTwo);
	 
			$data['math_question'] = "What is {$questionOne} + {$questionTwo}?";
			
			$this -> load -> view(F_MASTER . 'index', $data);
		}else{
			// Create users table
			$data_create_users = array('first_name' => $this -> input -> post('first_name',TRUE), 'last_name' => $this -> input -> post('last_name',TRUE), 'username' => $this -> input -> post('username',TRUE),'email' => $this -> input -> post('email',TRUE), 'password' => sha1($this -> input -> post('password',TRUE)), 'status' =>1, 'created_date' => null);
			// Save data information of user register to our system
			$is_saved = $this -> mod_globals -> insert(PREFIX_TABLE . "product_owners", $data_create_users);
			$this->mod_globals->insert(PREFIX_TABLE."subscribes",array('email'=>$this -> input -> post('email',TRUE),'status'=>1,'created_date	'=>null));
			if ($is_saved) {
				$this -> session -> set_flashdata('success', 'User  account was created success .');
				redirect('account/sign-in');
			} else {
				$this -> session -> set_flashdata('error', 'User  account was created fail .');
				redirect('account/sign-in');
			}
		}
	}

	// Function to help user to recover their password when they forgot thier password that they used to login their account to post or controll their product
	public function forgotten() {
		$data['title'] = "Forgotten Password";
		$this -> form_validation -> set_rules('email', 'Email', 'required|valid_email');
		if ($this -> form_validation -> run() === FALSE) {
			$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
			$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
			$this -> load -> view(F_MASTER . 'index', $data);
		} else {
			$email = $this -> input -> post('email',TRUE);
			// Checking the email that user provide does it has register in our system before or not before they can reset a new password form us.
			$is_exist_account = $this -> mod_globals -> check_data_exist(PREFIX_TABLE."product_owners",array('email'=>$email));
			if ($is_exist_account) {
				$user_id=$this->mod_globals->select_where(PREFIX_TABLE."product_owners",array('email'=>$email))->row()->product_owner_id;
				$pass_code = random_string('alnum', 25);
				$is_upd_pass_code = $this -> mod_globals -> update(PREFIX_TABLE."product_owners", array('token'=>$pass_code),array('email'=>$email));
				if ($is_upd_pass_code) {
					
					//send mail to user to get link for reset new password
					$this -> load -> library('email');
	
					$message = "Dear User,<br /> Recently you have has request to reset password from chatomuk website.<br />
					 if you are not request to us just ignore it.<br /> Otherwise Please click below to reset your new password.<br />
					 <a href='".base_url()."account/activate-new-password/".$pass_code."-UID".$user_id."'>".base_url()."account/activate-new-password/".$pass_code."-UID".$user_id."</a>" ;
					$this -> email -> from("info@chatomuk.com", "Kora");
					$this -> email -> to($email);
					$this -> email -> subject("Request reset new password");
					$this -> email -> message($message);
					$mailsend = $this -> email -> send();
					
					if($mailsend){
					
						$this -> session -> set_flashdata('success', 'Please check your email account to reset new password');
						redirect('account/forgotten');
					}else{
					
						$this -> session -> set_flashdata('error', 'Mail was sent fail,Please try again to reset your new password!');
						redirect('account/forgotten');
					}
				}else{
					
					$this -> session -> set_flashdata('error', 'Reset password  failed,Please try again !');
					redirect('account/forgotten');	
				}						
			} else {
				$this -> session -> set_flashdata('error', 'Your email account that you provide does not exist in our system yet,Please click  <a href="'.base_url().'account/sign-up">register</a> to create an account or contact to owner website about your problem.');
				redirect('account/forgotten');
			}
		}
	}

	//Function for checking link for reset new password that we provide when they request new password from us, to reset new password account
	public function activate_new_password(){
		$user_key=$this->uri->segment(3);
		//$jobs_title = $this -> uri -> segment(3);
		$key = $this->getBetween("@" . $user_key, "@", "-UID"); // Contant , start, and 
        $user_id = substr($user_key, strrpos($user_key, "D") + 1);
		// check is the link that user link to reset new password is correctly
		$is_user_request_psw=$this->mod_globals->select_where(PREFIX_TABLE."product_owners",array('token'=>$key,'product_owner_id'=>$user_id));
		if($is_user_request_psw->num_rows()>0){
			//$this->mod_globals->update(PREFIX_TABLE."product_owners",array('token'=>0));
			$this -> session -> set_flashdata('success', 'Your activation account was activated success .Please fill out this form  to create new password');
			$this->session->set_userdata("id_for_user_pass",$user_id);
			redirect("account/create-new-password");
		}else{
			$this -> session -> set_flashdata('error', 'Your have activated new password fail, Please try again or contact to owner website');
			redirect("account/forgotten");
		}
	}
	
	// Function for create new password after validation link reset correctly
	public function create_new_password(){
		$data['title']="Create new password";
		if(!$this->session->userdata("id_for_user_pass")){
			redirect('account');
		}
		$this -> form_validation -> set_rules('password', 'Password', 'trim|min_length[8]|max_length[50|matches[confirm_password]');
		$this -> form_validation -> set_rules('confirm_password', 'Confirm password', 'trim|min_length[8]|max_length[50]');

		if ($this -> form_validation -> run() === FALSE) {
			$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
			$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
			$this -> load -> view(F_MASTER . 'index', $data);
		}else{
			$user_id=$this->session->userdata("id_for_user_pass");
			$new_pass=$this->input->post("password",TRUE);
			
			//update new password for user
			$is_reset_pass=$this->mod_globals->update(PREFIX_TABLE."product_owners",array("password"=>sha1($new_pass)),array('product_owner_id'=>$user_id));
			if($is_reset_pass){
				//update token key to reset new password for user to null
				$this->mod_globals->update(PREFIX_TABLE."product_owners",array("token"=>NULL),array('product_owner_id'=>$user_id));
				// destroy seesion that set when validate link confirm reset password success
				$this->session->unset_userdata("id_for_user_pass");
				$this -> session -> set_flashdata('success', 'New password was created success');
				redirect("account");
			}else{
				$this -> session -> set_flashdata('error', 'New password was created fail, Please try again !');
				redirect("account/forgotten");
			}
		}
	}
	
	// function help to splite url with title and id of friendly url
	function getBetween($content, $start, $end) {
            $r = explode($start, $content);
            if (isset($r[1])) {
                $r = explode($end, $r[1]);
                return $r[0];
            }
            return '';
    }
	
	//  function using to update user account information
	public function update_account() {
		
		if(!$this->session->userdata('simple_authentication_id')){
			redirect('account');
		}
		$data['title']="Update Account";
		$data['mail_counts']=$this->mail_count;
		$user_id=$this->session->userdata('simple_authentication_id');
		
		//validation form for user update thier information with correct format we need
		$this -> form_validation -> set_rules('first_name', 'First Name', 'trim|required|min_length[2]|max_length[20]|alpha');
		$this -> form_validation -> set_rules('last_name', 'Last Name', 'trim|required|min_length[2]|max_length[20]|alpha');
		$this -> form_validation -> set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[30]');
		$this -> form_validation -> set_rules('email', 'email', 'required|valid_email');

		if ($this -> form_validation -> run() === FALSE) {
			$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
			$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
			
			// Select information that user will update to display depend on theire user id
			$data['user_profile_data']=$this->mod_globals->select_where(PREFIX_TABLE."product_owners",array('product_owner_id'=>$user_id));
			$this -> load -> view(F_MASTER . 'index', $data);
		}else{
			
			//checking email update if that email has in our system or not, if it has we skip but if new we update to new email
			$unique_email=$this->mod_globals->check_data_exist(PREFIX_TABLE."product_owners", $arr_where = array("email"=>$this->input->post('email',TRUE)));
			if($unique_email){
				$data_update_users = array('first_name' => $this -> input -> post('first_name',TRUE), 'last_name' => $this -> input -> post('last_name',TRUE), 'username' => $this -> input -> post('username',TRUE));	
			}else{
				$data_update_users = array('first_name' => $this -> input -> post('first_name',TRUE), 'last_name' => $this -> input -> post('last_name',TRUE), 'username' => $this -> input -> post('username',TRUE),'email' => $this -> input -> post('email',TRUE));
			}
			// update information of user
			$is_updated=$this->mod_globals->update(PREFIX_TABLE."product_owners",$data_update_users,array('product_owner_id'=>$user_id));
			if($is_updated){
				$this -> session -> set_flashdata('success', 'Your profile information was updated success');
				redirect("account/products");
			}else{
				$this -> session -> set_flashdata('error', 'Your profile information was updated fail');
				redirect("account/products");
			}
		}
	}

	// function that allow user to change thier password
	public function change_password() {
		if(!$this->session->userdata('simple_authentication_id')){
			redirect('account');
		}
		
		$data['title']="Change password";
		$data['mail_counts']=$this->mail_count;
		
		$this -> form_validation -> set_rules('password', 'Password', 'trim|required|min_length[8]|max_length[50]|matches[confirm_password]');
		$this -> form_validation -> set_rules('confirm_password', 'Confirm password', 'trim|required|min_length[8]|max_length[50]');

		if ($this -> form_validation -> run() === FALSE) {
			$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
			$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
			$this -> load -> view(F_MASTER . 'index', $data);
		}else{
			$user_id=$this->session->userdata('simple_authentication_id');
			
			$new_pass=$this->input->post("password",TRUE);
			
			//update new password that user modify depend on their user id that they have login
			$is_update_pass=$this->mod_globals->update(PREFIX_TABLE."product_owners",array("password"=>sha1($new_pass)),array('product_owner_id'=>$user_id));
			if($is_update_pass){
				$this -> session -> set_flashdata('success', 'New password was updated success');
				redirect("account/products");
			}else{
				$this -> session -> set_flashdata('error', 'New password was updated fail, Please try again !');
				redirect("account/products");
			}
		}
	}

    // Function that allow user to change their image profile
	public function change_image_profile(){
		if(!$this->session->userdata('simple_authentication_id')){
			redirect('account');
		}
		
		$data['title']="Change image profile";
		$data['mail_counts']=$this->mail_count;
			if (isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0) {
				
				// user provided a file to upload config
				$config['upload_path'] = './templates/front_end/images/users/';
				$config['allowed_types'] = 'gif|jpg|jpeg|png|bmp';
				$config['max_size'] = '1000';
				$config['max_width'] = '1024';
				$config['max_height'] = '768';
				$this -> load -> library('upload', $config);

				if (!$this -> upload -> do_upload()) {
					$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
					$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
					$data['error'] = $this -> upload -> display_errors();
					$this -> load -> view(F_MASTER . 'index', $data);
				} else {
					// in here is where things went according to plan.
					// file is uploaded, people are happy
					$data = $this -> upload -> data();
					$image_name = $data['file_name'];
					$user_id=$this->session->userdata('simple_authentication_id');
					
					//update image profile
					$is_updated=$this->mod_globals->update(PREFIX_TABLE."product_owners",array('img_profile'=>$image_name),array('product_owner_id'=>$user_id));
					if($is_updated){
						$this -> session -> set_flashdata('success', 'Your image profile was updated success');
						redirect("account/products");
					}else{
						$this -> session -> set_flashdata('error', 'Your image profile was updated success');
						redirect("account/products");
					}
				}
			}else{
				$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
				$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
				$this -> load -> view(F_MASTER . 'index', $data);
			}
		
	}

	// Function to modify user subscribe when user register with our website to post job
	public function newsletter() {
		
		$data['title'] = "Newsletter";
		$data['mail_counts']=$this->mail_count;
		if(!$this->session->userdata('simple_authentication_id')){
			redirect('account');
		}
		
		$this -> form_validation -> set_rules('subscribe', 'Subscribe', 'required');

		if ($this -> form_validation -> run() === FALSE) {
			$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
			$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
			
			// Select thier subscibe option that they are using now
			$data['subscirbe']=$this->mod_globals->select_string(PREFIX_TABLE."subscribes","status",array("email"=>$this -> mod_account -> get_logged_in_user_info() -> email));
			$this -> load -> view(F_MASTER . 'index', $data);
		}else{
			$subscribe=$this->input->post('subscribe',TRUE);
			if($subscribe=="yes"){
				$subscribe=1;
			}else{
				$subscribe=0;
			}
			// Update to modify their subscribe option
			$is_updated=$this->mod_globals->update(PREFIX_TABLE."subscribes",array("status"=>$subscribe),array("email"=>$this -> mod_account -> get_logged_in_user_info() -> email));
			if($is_updated){
					$this -> session -> set_flashdata('success', 'You have modify your subscribe success');
					redirect("account");
			}else{
					$this -> session -> set_flashdata('error', 'You have modify your subscribe fail');
					redirect("account");
			}
			
		}
		
	}

	// Function to allow user to sign up for subscriber with our website
	public function create_newsletter() {
		
		$data['title'] = "Create an newsletter";
		
		$tbl_subcribe=PREFIX_TABLE."subscribes";
		$this -> form_validation -> set_rules('email', 'Email', "required|valid_email|is_unique[$tbl_subcribe.email]");

		if ($this -> form_validation -> run() === FALSE) {
			$this->session->set_flashdata('errors', validation_errors());
			redirect();
		}else{
			$email_subscribe=$this->input->post('email',TRUE);
			// Save data to register new user subscribe
			$is_created=$this->mod_globals->insert(PREFIX_TABLE."subscribes",array("status"=>1,"email"=>$email_subscribe,"created_date"=>null));
			if($is_created){
					$this -> session -> set_flashdata('success', 'You have registered your subscribe success');
					redirect();
			}else{
					$this -> session -> set_flashdata('error', 'You have registered your subscribe fail ! Please try again');
					redirect();
			}
			
		}
		
	}

	// Function to allow user to modify thier contact information
	public function contact() {
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$data['title'] = "Contact Information";
		$data['mail_counts']=$this->mail_count;
		$this -> form_validation -> set_rules('country', 'Country', 'required');
		$this -> form_validation -> set_rules('location', 'Location', 'required');
		$this -> form_validation -> set_rules('phones', 'Phone', 'required');
		

		if ($this -> form_validation -> run() === FALSE) {
			$data['contact_information']=$this->mod_globals->select_where(PREFIX_TABLE."product_owner_contacts",array("use_product_owner_id"=>$this->session->userdata('simple_authentication_id')));
			if($data['contact_information']->num_rows()>0){
				$data['location_id_selected']=$data['contact_information']->row()->use_location_id;
				$data['country_id_selected']=$this->mod_globals->select_string(PREFIX_TABLE."locations","use_country_id",array('location_id'=>$data['contact_information']->row()->use_location_id));
				
			}else{
				$data['location_id_selected']=0;
				$data['country_id_selected']=0;
			}
			$data['countries']=$this->mod_globals->select_all(PREFIX_TABLE."countries");
			$data['locations']=$this->mod_globals->select_all(PREFIX_TABLE."locations");
			
			$data['phones']=$this->mod_globals->select_where(PREFIX_TABLE."product_owner_phones",array("use_product_owner_user_id"=>$this->session->userdata('simple_authentication_id')));
			
			$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
			$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
			$this -> load -> view(F_MASTER . 'index', $data);
		}else{
			$location=$this->input->post("location",TRUE);
			$fax=$this->input->post("fax",TRUE);
			$address=$this->input->post("address",TRUE);
			$phones=$this->input->post("phones",TRUE);
			
			//checking if the user have their contact already
			$isExistContact=$this->mod_globals->select_where(PREFIX_TABLE."product_owner_contacts",array("use_product_owner_id"=>$this->session->userdata('simple_authentication_id')));
			// if they have we modify ,or not we create new contact for them
			if($isExistContact->num_rows()>0){
				
				$this->mod_globals->update(PREFIX_TABLE."product_owner_contacts",array('use_location_id'=>$location,'fax'=>$fax,'address'=>$address),array("use_product_owner_id"=>$this->session->userdata('simple_authentication_id')));	
			}else{
				$this->mod_globals->insert(PREFIX_TABLE."product_owner_contacts",array('use_product_owner_id'=>$this->session->userdata('simple_authentication_id'),'use_location_id'=>$location,'fax'=>$fax,'address'=>$address));
			}
			
			// Insert or modify new phone number contact
			$this->mod_globals->delete(PREFIX_TABLE."product_owner_phones",array("use_product_owner_user_id"=>$this->session->userdata('simple_authentication_id')));
			foreach($phones as $phone){
				$this->mod_globals->insert(PREFIX_TABLE."product_owner_phones",array("use_product_owner_user_id"=>$this->session->userdata('simple_authentication_id'),'phone_number'=>$phone));
			}
			$this -> session -> set_flashdata('success', 'You have modified your contact information success');
			redirect("account/products");
		}
		
	}

	// Function to list all product of user post
	public function products(){
		$data['title'] = "User products";
		$data['mail_counts']=$this->mail_count;
		
		$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
		$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
		
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$user_id=$this->session->userdata('simple_authentication_id');
		$this->load->library('pagination');
		$config = array();
		$config["base_url"] = base_url() . "account/products";
		$config["total_rows"] = $this->mod_globals->select_join_count(PREFIX_TABLE."products", $arr_join = array(PREFIX_TABLE.'product_functions' => array('use_product_function_id' => 'product_function_id'),PREFIX_TABLE.'product_images' => array('product_id' => 'use_product_id')), $join_type ="INNER",array(PREFIX_TABLE."product_images.priority"=>1,'use_product_owner_id'=>$user_id));
		$config["per_page"] = 7;
		$config["uri_segment"] = 3;
		$config['full_tag_open'] = '<h3 class="pagination">';
		$config['full_tag_close'] = '</h3>';
	 
	    $this->pagination->initialize($config);
	 
	 	$tbl_products=PREFIX_TABLE."products";
	    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
	    $data['products']=$this->mod_globals->select_join_get_only_fields(PREFIX_TABLE."products",$arr_field=array(PREFIX_TABLE.'products.name AS pro_name','product_id','price','description',PREFIX_TABLE."product_functions.name AS fun_name",PREFIX_TABLE."product_images.name AS img_name"), $arr_join = array(PREFIX_TABLE.'product_functions' => array('use_product_function_id' => 'product_function_id'),PREFIX_TABLE.'product_images' => array('product_id' => 'use_product_id')), $join_type ="INNER",array(PREFIX_TABLE."product_images.priority"=>1,'use_product_owner_id'=>$user_id),$config["per_page"].",". $page,"product_id desc");
	    $data["pagination"] = $this->pagination->create_links();
	
		
		
		//$data['products']=$this->mod_globals->select_join(PREFIX_TABLE."products", $arr_join = array(PREFIX_TABLE.'product_functions' => array('use_product_function_id' => 'product_function_id')), $join_type ="INNER");
		$this -> load -> view(F_MASTER . 'index', $data);
	}

   // Function to list product of user that they post for sale
	public function product_for_sale(){
		$data['title'] = "User products";
		$data['mail_counts']=$this->mail_count;
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$user_id=$this->session->userdata('simple_authentication_id');
		$this->load->library('pagination');
		$config = array();
		$config["base_url"] = base_url() . "account/product-for-sale";
		$config["total_rows"] = $this->mod_globals->select_join_count(PREFIX_TABLE."products", $arr_join = array(PREFIX_TABLE.'product_functions' => array('use_product_function_id' => 'product_function_id'),PREFIX_TABLE.'product_images' => array('product_id' => 'use_product_id')), $join_type ="INNER",array(PREFIX_TABLE."product_functions.product_function_id"=>4,PREFIX_TABLE."product_images.priority"=>1,'use_product_owner_id'=>$user_id));
		$config["per_page"] = 10;
		$config["uri_segment"] = 3;
		$config['full_tag_open'] = '<h3 class="pagination">';
		$config['full_tag_close'] = '</h3>';
	 
	    $this->pagination->initialize($config);
	 
	    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
	    $data['products']=$this->mod_globals->select_join_get_only_fields(PREFIX_TABLE."products",$arr_field=array(PREFIX_TABLE.'products.name AS pro_name','product_id','key_code','price','description',PREFIX_TABLE."product_images.name AS img_name"),$arr_join = array(PREFIX_TABLE.'product_functions' => array('use_product_function_id' => 'product_function_id'),PREFIX_TABLE.'product_images' => array('product_id' => 'use_product_id')), $join_type ="INNER",array(PREFIX_TABLE."product_functions.product_function_id"=>4,PREFIX_TABLE."product_images.priority"=>1,'use_product_owner_id'=>$user_id),$config["per_page"].",". $page,"product_id desc");
	    $data["pagination"] = $this->pagination->create_links();
		
		//$data['products']=$this->mod_globals->select_join(PREFIX_TABLE."products", $arr_join = array(PREFIX_TABLE.'product_functions' => array('use_product_function_id' => 'product_function_id')), $join_type ="INNER",array(PREFIX_TABLE."product_functions.product_function_id"=>1),$config["per_page"].",". $page);
		$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
		$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
		$this -> load -> view(F_MASTER . 'index', $data);
	}
	
	// Function to list product of user that they post to find product to buy
	public function product_for_buy(){
		$data['title'] = "User products";
		$data['mail_counts']=$this->mail_count;
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$user_id=$this->session->userdata('simple_authentication_id');
		$this->load->library('pagination');
		$config = array();
		$config["base_url"] = base_url() . "account/product-for-buy";
		$config["total_rows"] = $this->mod_globals->select_join_count(PREFIX_TABLE."products", $arr_join = array(PREFIX_TABLE.'product_functions' => array('use_product_function_id' => 'product_function_id'),PREFIX_TABLE.'product_images' => array('product_id' => 'use_product_id')), $join_type ="INNER",array(PREFIX_TABLE."product_functions.product_function_id"=>5,PREFIX_TABLE."product_images.priority"=>1,'use_product_owner_id'=>$user_id));
		$config["per_page"] = 10;
		$config["uri_segment"] = 3;
		$config['full_tag_open'] = '<h3 class="pagination">';
		$config['full_tag_close'] = '</h3>';
	 
	    $this->pagination->initialize($config);
	 
	    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
	    $data['products']=$this->mod_globals->select_join_get_only_fields(PREFIX_TABLE."products",$arr_field=array(PREFIX_TABLE.'products.name AS pro_name','product_id','key_code','price','description',PREFIX_TABLE."product_images.name AS img_name"),$arr_join = array(PREFIX_TABLE.'product_functions' => array('use_product_function_id' => 'product_function_id'),PREFIX_TABLE.'product_images' => array('product_id' => 'use_product_id')), $join_type ="INNER",array(PREFIX_TABLE."product_functions.product_function_id"=>4,PREFIX_TABLE."product_images.priority"=>1,'use_product_owner_id'=>$user_id),$config["per_page"].",". $page,"product_id desc");
	    $data["pagination"] = $this->pagination->create_links();
	
		$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
		$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
		$this -> load -> view(F_MASTER . 'index', $data);
	}

	// This function use to post product to our website
	public function post_product(){
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$data['title'] = "Post new product";
		$data['mail_counts']=$this->mail_count;
		
		$this -> form_validation -> set_rules('name', 'Name', 'trim|required');

		if ($this -> form_validation -> run() === FALSE) {
			$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
			$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
			$data['product_functions']=$this->mod_globals->select_all(PREFIX_TABLE."product_functions");
			$this -> load -> view(F_MASTER . 'index', $data);
		}else{
			//$brand=$this->input->post("brand",TRUE);
			$category=$this->input->post("category",TRUE);
			$name=$this->input->post("name",TRUE);
			$price=$this->input->post("price",TRUE);
			$desc=$this->input->post("description",TRUE);
			$more_info=$this->input->post("more_info",TRUE);
			$product_for=$this->input->post("product_for",TRUE);
			$user_id=$this->session->userdata('simple_authentication_id');
			
			// random key code for product post with 12 charactor
			$key_code=random_string('alnum', 12);
			// checking random key exist before insert
			while ($this->isUsed($key_code)) {
			    $key_code = random_string('alnum', 12);
			}
			
			// static image upload. u can change it to dynamic depend on your upload file image
			$default_img="pic1.jpg";
			
			// Save your product to our system to display it on website
			$product_id=$this->mod_globals->insert_get_last_id(PREFIX_TABLE."products",array('use_product_owner_id'=>$user_id,'use_category_id'=>$category,'use_product_function_id'=>$product_for,'name'=>$name,'price'=>$price,'description'=>$desc,'more_info'=>$more_info,'key_code'=>$key_code,'view'=>0,'created_date'=>null));
			$isSaved=$this->mod_globals->insert(PREFIX_TABLE."product_images",array("use_product_id"=>$product_id,"name"=>$default_img,"priority"=>1,"status"=>1,"created_date"=>null));
			
			if($isSaved){
				$this -> session -> set_flashdata('success', 'New product was created success ! ');
				redirect("account/products");
			}else{
				$this -> session -> set_flashdata('success', 'New product was created fail !');
				redirect("account/products");
			}
		}
		
		
	}
	
	// function to help checking exist key code of product before insert to database
	function isUsed($key_code)
	{
		if(!$this->mod_globals->check_data_exist(PREFIX_TABLE."products", $arr_where = array("key_code"=>$key_code))){
			return FALSE;
		}else{
			return TRUE;
		}
	}
	//Function to modify their product information that they have post
	public function update_product(){
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$product_key=$this->uri->segment(3);
		//$jobs_title = $this -> uri -> segment(3);
		$pro_title = $this->getBetween("@" . $product_key, "@", "PID"); // Contant , start, and 
        $pro_id = substr($product_key, strrpos($product_key, "D") + 1);
		
		$data['title'] = "Update Product";
		$data['mail_counts']=$this->mail_count;
		
		$this -> form_validation -> set_rules('name', 'Name', 'trim|required');

		if ($this -> form_validation -> run() === FALSE) {
			
			// select information that we required to modify with  user product update
			$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
			$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
			$data['product_functions']=$this->mod_globals->select_all(PREFIX_TABLE."product_functions");
			$data['product_information']=$this->mod_globals->select_where(PREFIX_TABLE."products",array("product_id"=>$pro_id));
			$data['use_brand_id']=$this->mod_globals->select_string(PREFIX_TABLE."categories","use_brand_id",array('category_id'=>$data['product_information']->row()->use_category_id));
			
			$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
			$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
			$this -> load -> view(F_MASTER . 'index', $data);
		}else{
			//$brand=$this->input->post("brand",TRUE);
			$category=$this->input->post("category",TRUE);
			$name=$this->input->post("name",TRUE);
			$price=$this->input->post("price",TRUE);
			$desc=$this->input->post("description",TRUE);
			$more_info=$this->input->post("more_info",TRUE);
			$product_for=$this->input->post("product_for",TRUE);
			// Update product information that user have changed or modify
			$isSaved=$this->mod_globals->update(PREFIX_TABLE."products",array('use_category_id'=>$category,'use_product_function_id'=>$product_for,'name'=>$name,'price'=>$price,'description'=>$desc,'more_info'=>$more_info),array('product_id'=>$pro_id));
			if($isSaved){
				$this -> session -> set_flashdata('success', 'Your product was updated success ! ');
				redirect("account/products");
			}else{
				$this -> session -> set_flashdata('success', 'Your product was updated fail !');
				redirect("account/products");
			}
		}
		
	}

	//Function to delete product from their product controll that they have posted
	public function delete_product(){
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$product_key=$this->uri->segment(3);
		//$jobs_title = $this -> uri -> segment(3);
		$pro_name = $this->getBetween("@" . $product_key, "@", "PID"); // Contant , start, and 
        $pro_id = substr($product_key, strrpos($product_key, "D") + 1);
		// Delete the product post from our website
		$this->mod_globals->delete(PREFIX_TABLE."products",array('product_id'=>$pro_id));
		$this -> session -> set_flashdata('success', 'Your product was deleted success !');
		redirect("account/products");
	}
	
	// Function for list email user sent from other user
	public function mails(){
		$data['title'] = "User products";
		$data['mail_counts']=$this->mail_count;
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		
		$user_id=$this->session->userdata('simple_authentication_id');
		
		$this->load->library('pagination');
		$config = array();
		$config["base_url"] = base_url() . "account/mails";
		//$config["total_rows"] = $this->mod_globals->select_join_count(PREFIX_TABLE."product_owner_mailss", $arr_join = array(PREFIX_TABLE.'product_owners' => array('use_send_from_product_owner_id' => 'product_owner_id')), $join_type ="INNER",array('use_send_to_product_owner_id'=>$user_id));
		$config["total_rows"] = $this->mod_account->select_mail_count($user_id);	 
		
		$config["per_page"] = 30;
		$config["uri_segment"] = 3;
		$config['full_tag_open'] = '<h3 class="pagination">';
		$config['full_tag_close'] = '</h3>';
	 
	    $this->pagination->initialize($config);
	 
	    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$data['mail_list']=$this->mod_account->select_mail_list_limit($config['per_page'], $page,$user_id);
	    //$data['mail_list']=$this->mod_globals->select_join(PREFIX_TABLE."product_owner_mails", $arr_join = array(PREFIX_TABLE.'product_owners' => array('use_send_from_product_owner_id' => 'product_owner_id'),PREFIX_TABLE.'mail_replies' => array('product_owner_mail_id' => 'mail_reply_use_mail_id')), $join_type ="INNER",array('use_send_to_product_owner_id'=>$user_id),$config['per_page'].",". $page,"product_owner_mail_id desc");
	    $data["pagination"] = $this->pagination->create_links();
		
		
		//$data['products']=$this->mod_globals->select_all(PREFIX_TABLE."products");
		$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
		$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
		$this -> load -> view(F_MASTER . 'index', $data);
	}

    //Function used to view email detail of user send
	public function view_mail(){
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$data['title'] = "View Mail";
		
		$mail_id=$this->uri->segment(3);
		$user_id=$this->session->userdata('simple_authentication_id');
		$this->mod_globals->update(PREFIX_TABLE."product_owner_mails",array(PREFIX_TABLE."product_owner_mails.status"=>0),array("product_owner_mail_id"=>$mail_id));
		//$this->mail_count=$this->mod_globals->select_join_count(PREFIX_TABLE."product_owner_mails", $arr_join = array(PREFIX_TABLE.'product_owners' => array('use_send_from_product_owner_id' => 'product_owner_id')), $join_type ="INNER",array('use_send_to_product_owner_id'=>$user_id,'mail_status'=>1));
		$this->mail_count=$this->mod_account->select_count_mail_active($user_id);
		$data['mail_counts']=$this->mail_count;
		//$data['mail_info']=$this->mod_globals->select_join(PREFIX_TABLE."product_owner_mails", $arr_join = array(PREFIX_TABLE.'product_owners' => array('use_send_from_product_owner_id' => 'product_owner_id'),PREFIX_TABLE.'mail_replies' => array('product_owner_mail_id' => 'mail_reply_use_mail_id')), $join_type ="INNER",array('use_send_to_product_owner_id'=>$user_id,'product_owner_mail_id'=>$mail_id));
		$data['mail_info']=$this->mod_globals->select_join(PREFIX_TABLE."product_owner_mails", $arr_join = array(PREFIX_TABLE.'product_owners' => array('use_send_from_product_owner_id' => 'product_owner_id'),PREFIX_TABLE.'mail_replies' => array('product_owner_mail_id' => 'use_mail_id')), $join_type ="INNER",array('product_owner_mail_id'=>$mail_id));
		
		$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
		$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
		$this -> load -> view(F_MASTER . 'index', $data);
	}

   // Function to allow user to send mail to other user
	public function compose_mail(){
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$data['title'] = "Compose Mail";
		$data['mail_counts']=$this->mail_count;
		$this -> form_validation -> set_rules('send_to', 'To', 'required');
		$this -> form_validation -> set_rules('subject', 'Subject', 'required');
		$this -> form_validation -> set_rules('message', 'Message', 'required');

		if ($this -> form_validation -> run() === FALSE) {
			$data['mails']=$this->mod_globals->select_all(PREFIX_TABLE."product_owners");
			
			$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
			$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
			$this -> load -> view(F_MASTER . 'index', $data);	
		}else{
			$send_to=$this->input->post('send_to',TRUE);
			$subject=$this->input->post('subject',TRUE);
			$message=$this->input->post('message',TRUE);
			$user_id=$this->session->userdata('simple_authentication_id');
			
			$mail_to=$this->mod_globals->select_string(PREFIX_TABLE."product_owners","product_owner_id",array("email"=>$send_to));
		
			if($mail_to==""){
				$this -> session -> set_flashdata('warning', 'You can not send to other mail user beside  user that use our application !');
				redirect("account/mails");
			}
			
			$mail_id=$this->mod_globals->insert_get_last_id(PREFIX_TABLE."product_owner_mails",array('subject'=>$subject,'use_send_to_product_owner_id'=>$mail_to,'use_send_from_product_owner_id'=>$user_id,PREFIX_TABLE."product_owner_mails.status"=>1,'created_date'=>null));
			$this->mod_globals->insert(PREFIX_TABLE."mail_replies",array('use_mail_id'=>$mail_id,'use_product_owner_id'=>$mail_to,'message_reply'=>$message,'created_date'=>null));
			$this -> session -> set_flashdata('success', 'Your message have been sent success !');
			redirect("account/mails");
		}
		
	}

	//Function use to reply email to other user
	public function reply_mail(){
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$mail_id=$this->uri->segment(3);
		$message=$this->input->post('message',TRUE);
		
		$send_from=$this->mod_globals->select_string(PREFIX_TABLE."product_owner_mails","use_send_from_product_owner_id",array("product_owner_mail_id"=>$mail_id));
		$this->mod_globals->insert(PREFIX_TABLE."mail_replies",array("use_mail_id"=>$mail_id,"use_product_owner_id"=>$send_from,"message_reply"=>$message,"created_date"=>null));
		
		$this->mod_globals->update(PREFIX_TABLE."product_owner_mails",array(PREFIX_TABLE."product_owner_mails.status"=>1,"is_reply"=>1),array("product_owner_mail_id"=>$mail_id));
		$this -> session -> set_flashdata('success', 'Your message have been sent success !');
		redirect("account/mails");
		
	}
	
	//Function use to remove email from thier inboxs
	public function delete_mail(){
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$mail_id=$this->uri->segment(3);
		$get_send_to_from=$this->mod_globals->select_where(PREFIX_TABLE."product_owner_mails",array("product_owner_mail_id"=>$mail_id));
		
		$send_from_user_id=$get_send_to_from->row()->use_send_from_product_owner_id;
		$send_to_user_id=$get_send_to_from->row()->use_send_to_product_owner_id;
		
		$user_id=$this -> mod_account -> get_logged_in_user_info() -> product_owner_id;
		if($send_from_user_id==$user_id){
			$this->mod_globals->update(PREFIX_TABLE."product_owner_mails",array('is_send_from_deleted'=>$user_id),array("product_owner_mail_id"=>$mail_id));
		}else if($send_to_user_id==$user_id){
			$this->mod_globals->update(PREFIX_TABLE."product_owner_mails",array('is_send_to_deleted'=>$user_id),array("product_owner_mail_id"=>$mail_id));
		}
		
		//$this->mod_globals->delete(PREFIX_TABLE."product_owner_mails",array("product_owner_mail_id"=>$mail_id));
		$this -> session -> set_flashdata('success', 'Your message was deleted success !');
		redirect("account/mails");
		
	}
	public function feedback(){
		$data['title'] = "Feedback";
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$data['products']=$this->mod_globals->select_all(PREFIX_TABLE."products");
		$data['categories']=$this->mod_globals->select_all(PREFIX_TABLE."categories");
		$data['brands']=$this->mod_globals->select_all(PREFIX_TABLE."brands");
		$this -> load -> view(F_MASTER . 'index', $data);
	}
	public function delete_feedback(){
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$data['title'] = "Welcome";
		$this -> load -> view(F_MASTER . 'index', $data);
	}
	
	// Function to exit from their controll product
	public function logout(){
		if (!$this -> mod_account -> is_logged_in()) {
			redirect("account/sign-in");
		}
		$this->mod_account->logout();
	}
	
	// Function help to select list location depend on country that user select
	public function get_location_by_country(){
		$country_id=$this->input->post('country_id',TRUE);
		$locations=$this->mod_globals->select_where(PREFIX_TABLE."locations",array('use_country_id'=>$country_id));
		foreach($locations->result() as $location){
			$location_id=$location->location_id;
			$location_name=$location->name;
			?><option value="<?php echo $location_id; ?>"><?php echo $location_name; ?></option>";<?php
		}
	}

	// Function to help checking correct math question with value that user input
	function math_question()
	{
		$user_answer = $this->input->post('math_question');
		$questionOne = $this->session->userdata('questionOne');
		$questionTwo = $this->session->userdata('questionTwo');
 
		$correct_answer = $questionOne + $questionTwo;
 
		if($user_answer != $correct_answer)
		{
			$this->form_validation->set_message('math_question', 'Your answer to the math question was not corrected !');
			return FALSE;
		} else {
			return TRUE;
		}
	}
}
